home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_02_07 / 2n07028a < prev    next >
Text File  |  1991-05-31  |  6KB  |  201 lines

  1.  
  2. LISTING.1
  3.  
  4.  
  5. #include <os2def.h>
  6. #include <io.h>
  7. #include <fcntl.h>
  8. #include <bios.h>
  9. #include <dos.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #pragma pack(1)
  15. //
  16.  
  17. /***************************************************************************/
  18. /* #defines                                                                */
  19. /***************************************************************************/
  20. #define CHUNK 0x2000L
  21.  
  22. #define SUPER_BLOCK 0x10
  23.  
  24. #define FIXED_DRIVE 0x81
  25. #define HPFS_PART 7
  26.  
  27. #define  DOWN (-1)
  28. #define  SAME 0
  29. #define  UP 1
  30.  
  31. #define STDOUT 1
  32.                                  // Return codes to DOS
  33. #define NO_ARG    1
  34. #define BAD_DISK  2
  35. #define NO_FILE   3
  36. #define IO_ERR    4
  37. //
  38.  
  39. /***************************************************************************/
  40. /* structures                                                              */
  41. /***************************************************************************/
  42. typedef ULONG RSN;               // Relative Sector Number
  43.  
  44. typedef struct {
  45.    BYTE  BootInd;
  46.    BYTE  BH;
  47.    BYTE  BS;
  48.    BYTE  BCyl;
  49.    BYTE  SysInd;
  50.    BYTE  EH;
  51.    BYTE  ES;
  52.    BYTE  ECyl;
  53.    RSN   PartitionOffset;
  54.    ULONG NumSect;
  55. } PartitionEntry;
  56.  
  57. static struct {
  58.    char  BootCode [0x1BE];
  59.    PartitionEntry Partitions[4];
  60.    USHORT Signature;
  61. } VolumeBootSector;
  62.  
  63. static struct {
  64.    char  JMP[3];
  65.    char  OEM[8];
  66.    USHORT BytesPerSector;
  67.    BYTE  SectorsPerCluster;
  68.    USHORT ReservedSectors;
  69.    BYTE  NumOfFATs;
  70.    USHORT NumRootDirEntries;
  71.    USHORT SectorsInMedia;
  72.    char  MediaDescriptor;
  73.    USHORT SectorsIn1FAT;
  74.    USHORT SectorsInTrack;
  75.    USHORT NumOfHeads;
  76.    ULONG NumOfHiddenSectors;
  77.    ULONG LongSectorsInMedia;
  78.    char  BootCode[0x1DE];
  79. } PartitionBootSector;
  80.  
  81. typedef struct {
  82.    USHORT uscDirEntrySize;       // 00
  83.    BYTE  bSplitFlag;             // 02
  84. #define     DIR_SPLITS  4
  85. #define     END_OF_DIR  8
  86.    BYTE  bFlags;                 // 03
  87. #define     FLAG_DIR    0x10
  88.    RSN   ulPointedSector;        // 04
  89.    ULONG ulDate1;                // 08
  90.    ULONG ulcFileLength;          // 0C
  91.    ULONG ulDate2;                // 10
  92.    ULONG ulDate3;                // 14
  93.    BYTE  bReserved2[6];          // 18
  94.    BYTE  bcNameLength;           // 1E
  95.    BYTE  bName[1];               // 1F    Variable length item
  96. } DirEntryType;                  //       Formal length 0x20 bytes
  97.  
  98. typedef struct {
  99.    ULONG ulMagicNumber;          // 00
  100.    BYTE  bReserved1[8];          // 04
  101.    BYTE  bcEntryNameLen;         // 0C
  102.    BYTE  sEntryName[0xF];        // 0D
  103.    RSN   ulParentSector;         // 1C
  104.    BYTE  bReserved2[0x14];       // 20
  105.    USHORT uscExtendedAttributes; // 34
  106.    BYTE  bReserved3;             // 36
  107.    BYTE  bEntryType;             // 37
  108.    BYTE  bFragFileInd;           // 38
  109. #define     MANY_FRAGMENTS 0x80
  110.    BYTE  bReserved4[4];          // 39
  111.    BYTE  bcFragNum;              // 3D
  112.    BYTE  bReserved5[2];          // 3E
  113.    union {
  114.       struct {
  115.          ULONG ulcFragSectors;
  116.          RSN   ulFragSector;
  117.       }  ManyFrags[0xC];         // 40
  118.       struct {
  119.          ULONG ulcFragSoFar;
  120.          ULONG ulcFragSectors;
  121.          RSN   ulFragSector;
  122.       }  FewFrags[8];            // 40
  123.    } FragTable;
  124.    ULONG ulcFileLength;          // 58
  125.    BYTE  Reserved6[0x1A4];       // 5C
  126. } EntryFnodeType;
  127.  
  128. typedef struct {
  129.    ULONG ulMagicNumber;          // 00
  130.    BYTE  bReserved1[4];          // 04
  131.    BYTE  bTopOfDir;              // 08
  132. //#define      IS_TOP      1     //       Bit on in "root" fnode of directory
  133.    BYTE  bReserved2[3];          // 09
  134.    RSN   ulParentSector;         // 0C
  135.    RSN   ulMyself;               // 10
  136.    DirEntryType FirstDirEntry;   // 14    Formal length 0x20 bytes
  137.    BYTE  bReserved3[0x7CC];      // 34
  138. } DirectoryClusterType;
  139.  
  140. typedef struct {
  141.    ULONG ulMagicNumber;          // 00
  142.    RSN   ulMyself;               // 04
  143.    RSN   ulParentSector;         // 08
  144.    BYTE  bFragFileInd;           // 0C
  145.    BYTE  bReserved1[4];          // 0D
  146.    BYTE  bcFragNum;              // 11
  147.    BYTE  bReserved2[2];          // 12
  148.    struct {                      // 14
  149.       ULONG ulcFragSoFar;
  150.       ULONG ulcFragSectors;
  151.       RSN   ulFragSector;
  152.    } FragTable[1];               //       Formal length  0xC bytes
  153.    BYTE  bReserved[0x7E0];       // 20
  154. } FragmentedFileTableType;
  155.  
  156. typedef struct {
  157.    BYTE  bReserved1[0xC];        // 00
  158.    RSN   RootDirFnode;           // 0C
  159.    BYTE  bReserved2[0x1F0];      // 10
  160. } SuperBlockType;
  161. //
  162.  
  163. /***************************************************************************/
  164. /* global variables                                                        */
  165. /***************************************************************************/
  166. static struct diskinfo_t DiskInfo;
  167. static PartitionEntry *HPFSPartition;
  168. static USHORT BytesPerSector = sizeof (VolumeBootSector);
  169. static USHORT SectorsInFullTrack;
  170. static USHORT SectorsInSegment;
  171. static USHORT HugeShift;
  172. static RSN RootDirSector;
  173. static EntryFnodeType~r DirFnode;
  174. static DirectoryClusterType DirectoryCluster;
  175. static SuperBlockType SuperBlock;
  176. static union {
  177.    FragmentedFileTableType ManyFragsFnode;
  178.    EntryFnodeType FewFragsFnode;
  179. } FileFnode;
  180. static USHORT Level = 0;
  181. static ULONG FileSize;
  182. static char FileBuff[CHUNK];
  183. //
  184.  
  185. /***************************************************************************/
  186. /* function definitions                                                    */
  187. /***************************************************************************/
  188. void _bios_disk_with_retry (struct diskinfo_t *Info);
  189. void DoFewFrags (EntryFnodeType *Fnode);
  190. void DoManyFrags (FragmentedFileTableType *FTable);
  191. void DoSectors (ULONG FragSize, RSN FragSector);
  192. BOOL FindDirEntry (char *EntryName, DirEntryType **Location);
  193. void FindFile (const char *path);
  194. void FindFileFnode (char *);
  195. void GetBootRecords (void);
  196. void GetRootDir (void);
  197. void GetRelativeSectors (RSN SectorLoc, USHORT SectorNum, void *Into);
  198. int main (int argc, char *argv[]);
  199. void MakeFilename (char *path, char *dir, char *filname);
  200. //
  201.